ifeq ($(OS),Windows_NT)
    CC = gcc
	CFLAGS = -std=c99 -Wall -Wextra -DWIN32 -g
    RM = del /q
    EXE_EXT = .exe
	OBJ_EXT = .obj
    MKDIR = mkdir
else
    CC = gcc
	CFLAGS = -std=c99 -Wall -Wextra -DUNIX -g
    RM = rm -f
    EXE_EXT = 
	OBJ_EXT = .o
    MKDIR = mkdir -p
endif

SRC_FILES = main.c \
			Parser/tokenizer.c \
			Parser/parser.c \
			Executor/executePlan.c \
			Executor/executor.c \
			Store/database.c \
			Store/BPlusTree.c \
			Store/pager.c \
			Store/table.c \
			MyError/myerr.c \
			util.c

OBJ_FILES = $(SRC_FILES:.c=$(OBJ_EXT))

TEST_FILES = test_table \
			 test_one_page \
			 test_tokenizer \
			 test_parser \
			 test_metacmd \
			 test_plan \
			 test_exec

TARGET = bookDB$(EXT)

# target
all: $(TARGET) 

$(TARGET): $(OBJ_FILES)
	$(CC) $(CFLAGS) $^ -o $@ -I. -IParser -IExecutor -IStore -IMyError

%$(OBJ_EXT): %.c
	$(CC) $(CFLAGS) -c $< -o $@ -I. -IParser -IExecutor -IStore -IMyError

# test
test_tokenizer: Test/test_tokenizer.c Parser/tokenizer.c MyError/myerr.c
	$(CC) $(CFLAGS) $^ -o $@ -I. -IParser -IMyError

test_parser: Test/test_parser.c Parser/tokenizer.c Parser/parser.c MyError/myerr.c
	$(CC) $(CFLAGS) $^ -o $@ -I. -IParser -IMyError

test_one_page: Test/test_one_page.c Store/pager.c util.c MyError/myerr.c
	$(CC) $(CFLAGS) $^ -o $@ -I. -IStore -IMyError

test_table: Test/test_table.c  Store/BPlusTree.c Store/pager.c Store/table.c util.c MyError/myerr.c
	$(CC) $(CFLAGS) $^ -o $@ -I. -IStore -IMyError

test_metacmd: Test/test_metacmd.c Executor/executor.c Parser/parser.c Parser/tokenizer.c MyError/myerr.c
	$(CC) $(CFLAGS) $^ -o $@ -I. -IExecutor -IParser -IMyError

test_plan: Test/test_plan.c Executor/executePlan.c Parser/parser.c Parser/tokenizer.c MyError/myerr.c
	$(CC) $(CFLAGS) $^ -o $@ -I. -IExecutor -IParser -IMyError

test_exec: Test/test_exec.c Executor/executePlan.c Parser/parser.c Parser/tokenizer.c  \
		   Executor/executor.c Store/table.c Store/pager.c Store/database.c util.c \
		   Store/BPlusTree.c MyError/myerr.c
	$(CC) $(CFLAGS) $^ -o $@ -I. -IExecutor -IParser -IStore -IMyError

# clean
clean:
	$(RM) $(TARGET) $(OBJ_FILES) $(TEST_FILES)

.PHONY: clean all $(TARGET) $(TEST_FILES)
